package gdut.stream.last;

import gdut.stream.middle.Student;

import java.util.*;
import java.util.stream.Collectors;

public class CollectTest {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        ArrayList<String> books = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            books.add("book" + i);
        }
        for (int i = 0; i < 5; i++) {
            Student student = new Student(10 + i, "name" + i, books);
            students.add(student);
        }
        students.add(new Student(10, "name0", books));
//        获取一个存放所有作者名的list集合
        List<String> nameList = students.stream()
                .map(student -> student.getName())
                .collect(Collectors.toList());
        System.out.println(nameList);
//        获取一个存放所有书名的set集合
        Set<String> bookSet = students.stream()
                .flatMap(student -> student.getBooks().stream())
                .collect(Collectors.toSet());
        System.out.println(bookSet);
//        获取一个有作者名和作者的书籍的map(key,value)，注意要去重，key不能重复
        Map<String, List<String>> resultMap = students.stream()
                .distinct()
                .collect(Collectors.toMap(student -> student.getName(),student->student.getBooks()));
        Set<Map.Entry<String, List<String>>> entrySet = resultMap.entrySet();
        Iterator<Map.Entry<String, List<String>>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
